﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;
using Newtonsoft.Json.Linq;

namespace ArtLand.OA.Project.ADM
{
    public partial class Adm_ExpnseList : CommonPageBase
    {


        /// <summary>
        /// 取得权限
        /// </summary>
        /// <returns></returns>
        public bool GetQX()
        {

            bool n = true;
            string UserKeys = UserPower.GetKeyHaveKey("Adm_ExpnseIF", CookInfo.EAdminid(), CookInfo.EAdminGroupId());
            if (!UserKeys.Contains("View"))
            {
                PageContext.RegisterStartupScript("alert('您未有查看权限！');RemTab('dynamic_added_tab_tab_" + UserPower.GetKeyWord() + "');");
                n = false;
            }
            else
            {
                if (!UserKeys.Contains("Audit"))
                {
                    Grid1.Columns[Grid1.Columns.Count - 1].Hidden = true;
                }
            }
            return n;
        }
        private void OutputSummaryData(bool ts)
        {
            decimal d = TbsDbSession.Default.From<Adm_EXPNSE>().Select(Adm_EXPNSE._.AMOUNT.Sum()).Where(Adm_EXPNSE._.STATUS == 1 && GetWhereAll()).ToScalar<decimal>();
           


            float donateTotal = 0.0f;
            //foreach (DataRow row in source.Rows)
            //{
            //    donateTotal += Convert.ToInt32(row["Donate"]);
            //    feeTotal += Convert.ToInt32(row["Fee"]);
            //}
            donateTotal = float.Parse(d.ToString());
            JObject jo = new JObject();
            jo.Add("donateTotal", donateTotal);

            t = jo.ToString(Newtonsoft.Json.Formatting.None);
            if (ts == false)
                PageContext.RegisterStartupScript("customsummary('" + t + "','" + Grid1.ClientID + "');");
        }
        public string t = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Enabled = false;
            Button2.Enabled = false;
            if (!IsPostBack)
            {
                if (GetQX())
                {
                    btnNew.OnClientClick = Window1.GetShowReference("Adm_ExpnseChange.aspx?cid=" + Request["did"], "申请行政费用");
                    BindGrid(GetWhere());
                    OutputSummaryData(true);
                }
            }
        }


        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.RecordCount = TbsDbSession.Default.From<Adm_EXPNSE>().Where(wc).Count();

            List<Adm_EXPNSE> items = TbsDbSession.Default.From<Adm_EXPNSE>().Where(wc).OrderBy(Adm_EXPNSE._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
            OutputSummaryData(false);
        }

        public WhereClip GetWhereAll()
        {
            WhereClip wc = WhereClip.All;
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (Adm_EXPNSE._.USRNAM.Contain(ttbxMyBox2.Text) || Adm_EXPNSE._.MoneyType.Contain(ttbxMyBox2.Text));
            }

            if (!String.IsNullOrEmpty(txt_StartTime1.Text))
            {
                wc = wc && Adm_EXPNSE._.PLDATE > Convert.ToDateTime(txt_StartTime1.Text);
            }
            if (!String.IsNullOrEmpty(txt_EndTime1.Text))
            {
                wc = wc && Adm_EXPNSE._.PLDATE < Convert.ToDateTime(txt_EndTime1.Text);
            }

            return wc;
        }


        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!string.IsNullOrEmpty(Request["did"]))
            {
                wc = wc && Adm_EXPNSE._.SUBTYP == int.Parse(Request["did"]);
            }
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (Adm_EXPNSE._.USRNAM.Contain(ttbxMyBox2.Text) || Adm_EXPNSE._.MoneyType.Contain(ttbxMyBox2.Text));
            }

            if (!string.IsNullOrEmpty(Request["y"]))
            {
                wc = wc && Adm_EXPNSE._.PLDATE.Year == int.Parse(Request["y"]);
            }
            if (!string.IsNullOrEmpty(Request["s"]))
            {
                wc = wc && Adm_EXPNSE._.STATUS == 0;
            }
            if (!String.IsNullOrEmpty(txt_StartTime1.Text))
            {
                wc = wc && Adm_EXPNSE._.PLDATE > Convert.ToDateTime(txt_StartTime1.Text);
            }
            if (!String.IsNullOrEmpty(txt_EndTime1.Text))
            {
                wc = wc && Adm_EXPNSE._.PLDATE < Convert.ToDateTime(txt_EndTime1.Text);
            }
           // wc = wc && (Adm_EXPNSE._.AuditEmpNO.Contain("," + CookInfo.EEmpID() + ",") || Adm_EXPNSE._.Adduid == CookInfo.EAdminid());


            return wc;
        }
        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid(GetWhere());
                OutputSummaryData(false);
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid(GetWhere());
            OutputSummaryData(false);
        }
        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string s = e.Values[8].ToString().Trim();
            if (s == "0")
            {
                s = "未审批";
            }
            else
            {
                if (s == "1")
                {
                    s = "已审批";
                    e.Values[9] = s;
                }
                else
                {
                    s = "审批拒绝";
                    e.Values[9] = s;
                }
            }
            e.Values[8] = s;
            //string color = e.Values[7].ToString();
            //string bold = e.Values[8].ToString();
            //if (bold == "True")
            //    bold = "bold";
            //else
            //    bold = "";
            //string title = "<span style=\"color:{0};font-weight:{1}\">" + e.Values[0] + "</span>";
            //title = string.Format(title, color, bold);
            //e.Values[0] = title;
            //e.Values[7] = "<a href=\"javascript:;\" onclick=\"TabView('/AfterSale/ASale_RePairChange.aspx?id=" + e.Values[7] + "', 'tab_wxadd', '修改维修记录', '/icon/comment_edit.png');\"><img src=\"/icon/comment_edit.png\" /></a>";
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Audit")
            {
                Proj_Sample tea = new Proj_Sample();
                tea.ID = id;
                tea.Attach();
                tea.STATUS = 1;
                TbsDbSession.Default.Update<Proj_Sample>(tea);
                BindGrid(GetWhere());
                //TbsDbSession.Default.Delete<Proj_Sample>(Proj_Sample._.ID == id);
                //Alert.ShowInTop("删除成功！");
                //BindGrid(GetWhere());
            }
            //if (e.CommandName == "CheckBox1")
            //{
            //    CheckBoxField checkField = (CheckBoxField)Grid1.FindColumn(e.ColumnIndex);
            //    bool checkState = checkField.GetCheckedState(e.RowIndex);
            //    Proj_Custor tea = new Proj_Custor();
            //    tea.ID = id;
            //    tea.Attach();
            //    tea.COMBIN = checkState;
            //    TbsDbSession.Default.Update<Proj_Custor>(tea);
            //    BindGrid(GetWhere());
            //}
        }

        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            int st = int.Parse(Grid1.DataKeys[e.RowIndex][1].ToString());
            if (st > 0)
            {

            }
            else

                PageContext.RegisterStartupScript(Window1.GetShowReference("Adm_ExpnseChange.aspx?id=" + id, "修改"));
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool b = true;
            int[] list = Grid1.SelectedRowIndexArray;
            if (list.Length > 0)
            {
                int index = list[0];
                int st = int.Parse(Grid1.DataKeys[index][1].ToString());
                if (st > 0)
                {
                    b = false;
                }
            }

            if (b)
                FormInfo.GridEdit(Grid1, Window1, "Adm_ExpnseChange.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            FormInfo.GridDelete<Adm_EXPNSE>(Grid1, Window1, new Adm_EXPNSE());
            BindGrid(GetWhere());
        }

        protected void Grid1_RowSelect(object sender, GridRowSelectEventArgs e)
        {
            int st = int.Parse(Grid1.DataKeys[e.RowIndex][1].ToString());
            if (st > 0)
            {
                Button1.Enabled = false;
                Button2.Enabled = false;
            }
            else
            {
                Button1.Enabled = true;
                Button2.Enabled = true;
            }
        }

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            BindGrid(GetWhere());
            OutputSummaryData(false);
        }
    }
}